Subject: Re: allocating 0 bytes with malloc()?
From: [email-masked] (Paul Sander)
Date: 27 Feb 1994 02:37:46 -0800
Newsgroups: comp.lang.c,comp.std.c

In article <[email-masked]>,
Robert Corbett <[email-masked]> wrote:
>>In article <[email-masked]>,
>>
>>Non-portable code:
>>
>>	ptr = malloc(size);
>>	if(ptr==0 && size!=0) fail...
>>
>>Portable code:
>>
>>	if(size == 0)
>>		ptr = NULL;
>>	else {
>>		ptr = malloc(size);
>>		if(ptr==0) fail...
>>	}
>>
>>I don't see the problem.
>
>Merriam Webster's Collegiate Dictionary, tenth edition, defines the word
>obstacle as "something that impedes progress or achievement."  Note the
>word "impedes."  An obstacle need not prevent progress or achievement, it
>need only impede it.

Most folks I know who are concerned about this issue have this macro in
their portability library:

#define myMalloc(x) ( (x) ? malloc(x) : (void*) 0 )

They don't seem to consider malloc's behavior in this regard to be much
of an obstacle.  And those that don't like their system's implementation
of malloc for whatever reasons replace it anyway; there are lots of free
implementations of it around and it's easy to add one to a link line.

I guess I work on a different set of problems than Mr. Corbett; I've
never needed to pass a 0 argument to malloc.  Instead, I've always found
ways to optimize away the need to allocate no memory.  But I would be
interested in reading (via email) about cases where this technique
substantially simplified the code.
-- 
Paul M. Sander  (408) 379-7000  |  "You are in a maze of twisty little
HaL Computer Systems, Inc.      |   methods, all just a little different."
1315 Dell Avenue                |   
Campbell, CA  95008  USA        |